<template>
  <!-- 线路订单操作按钮 -->
  <div class="lineOrder-handleBtn">
    <template v-if="setBtns(data).length !== 0">
      <!-- 列表展示 -->
      <template v-if="type === 'list'">
        <a-dropdown>
          <a-menu slot="overlay">
            <a-menu-item
              v-for="item in setBtns(data)"
              :key="item.flag"
              @click="clickEvent(item.clickEvent, data)"
            >
              {{ item.name }}
            </a-menu-item>
          </a-menu>
          <a-button size="small">操作 <a-icon type="down"/></a-button>
        </a-dropdown>
      </template>
      <!-- 详情展示 -->
      <template v-else>
        <a-space>
          <c-button
            v-for="item in setBtns(data)"
            :key="item.flag"
            ghost
            type="primary"
            @click="clickEvent(item.clickEvent, data)"
          >
            {{ item.name }}
          </c-button>
        </a-space>
      </template>
    </template>
    <c-modal
      title="订单备注"
      v-model="remarkVisible"
      :okLoading="remarkLoading"
      @ok="remarkSubmit"
      @cancel="cancelModal"
    >
      <a-textarea
        v-model="remark"
        placeholder="请输入订单备注内容"
      ></a-textarea>
    </c-modal>
  </div>
</template>

<script>
import orderDict from "@/dict/order";
import setBtns from "./btnJudge.js";
import {
  customerremarklineorder,
  providerremarklineorder,
  customeracceptalterlineorderpremium,
  customerrejectalterlineorderpremium,
  customeracceptalterlineorder,
  customerrejectalterlineorder,
  customeracceptalterlineorderfirmprice,
  customerrejectalterlineorderfirmprice,
  customeracceptalterlineorderbaseprice,
  customerrejectalterlineorderbaseprice
} from "@/api/order-line";

export default {
  props: {
    // 订单信息
    data: {
      type: Object
    },
    // list：订单列表 detail：订单详情 用于展示不同按钮排版
    type: {
      type: String,
      default: "list"
    }
  },
  data() {
    return {
      orderDict,
      handleRow: undefined, // 操作的row
      remark: undefined, // 订单备注
      remarkVisible: false, // 订单备注弹窗
      remarkType: undefined, // 备注订单类型 0买家 1卖家
      remarkLoading: false // 备注订单提交loading
    };
  },
  methods: {
    setBtns,
    clickEvent(event, row) {
      this[event](row, this);
    },
    // 关闭弹窗回调
    cancelModal() {
      this.handleRow = undefined;
      this.remark = undefined;
      this.partDealAmount = undefined;
      this.modifyPriceAmount = undefined;
      this.remarkType = undefined;
    },
    // 备注订单提交
    remarkSubmit() {
      this.remarkLoading = true;
      let request =
        this.remarkType === 0
          ? customerremarklineorder
          : providerremarklineorder;
      request({
        id: this.handleRow.id,
        version: this.handleRow.version,
        remark: this.remark
      })
        .then(res => {
          this.remarkVisible = false;
          this.$emit("handleSuccess", {
            data: res,
            type:
              this.remarkType === 0
                ? "buyerLineOrderRemark"
                : "sellerLineOrderRemark"
          });
        })
        .finally(() => {
          this.remarkLoading = false;
        });
    },
    // 买家备注订单
    buyerRemarkLineOrder(row) {
      this.handleRow = row;
      this.remarkVisible = true;
      this.remarkType = 0;
    },
    // 卖家备注订单
    sellerRemarkLineOrder(row) {
      this.handleRow = row;
      this.remarkVisible = true;
      this.remarkType = 1;
    },
    modifyPremium(type, row) {
      let request =
        type === 0
          ? customeracceptalterlineorderpremium
          : customerrejectalterlineorderpremium;
      request({
        id: row.id,
        version: row.version
      }).then(res => {
        this.$message.success("操作成功");
        this.$emit("handleSuccess", {
          data: res,
          type:
            type === 0
              ? "buyerAcceptModifyPremiumLine"
              : "buyerRejectModifyPremiumLine"
        });
      });
    },
    // 买家同意修改点价升贴水
    buyerAcceptModifyPremiumLine(row) {
      this.modifyPremium(0, row);
    },
    // 买家拒绝修改点价升贴水
    buyerRejectModifyPremiumLine(row) {
      this.modifyPremium(1, row);
    },
    modifyGoods(type, row) {
      let request =
        type === 0
          ? customeracceptalterlineorder
          : customerrejectalterlineorder;
      request({
        id: row.id,
        version: row.version
      }).then(() => {
        this.$message.success("操作成功");
        this.$emit("handleSuccess", {
          // data: res,
          type:
            type === 0
              ? "buyerAcceptModifyGoodsLine"
              : "buyerRejectModifyGoodsLine"
        });
      });
    },
    // 买家同意卖家修改线路订单申请
    buyerAcceptModifyGoodsLine(row) {
      this.modifyGoods(0, row);
    },
    // 买家拒绝修改订单申请
    buyerRejectModifyGoodsLine(row) {
      this.modifyGoods(1, row);
    },
    // 同意修改基价/含税单价
    acceptSellerModifyPrice(data, type, request) {
      let _this = this;
      this.$confirm({
        title: "此操作将修改该订单价格, 是否继续?",
        onOk() {
          return new Promise((resolve, reject) => {
            request({
              orderId: data.id,
              version: data.version
            })
              .then(res => {
                _this.$message.success("操作成功");
                _this.$emit("handleSuccess", {
                  data: res,
                  type: type
                });
                resolve();
              })
              .catch(() => {
                reject();
              });
          });
        }
      });
    },
    // 拒绝修改基价/含税单价
    rejectSellerModifyPrice(data, type, request) {
      request({
        orderId: data.id,
        version: data.version
      }).then(res => {
        this.$message.success("操作成功");
        this.$emit("handleSuccess", { data: res, type: type });
      });
    },
    // 买家同意修改线路订单基价
    acceptSellerModifyBasePriceByLine(data) {
      this.acceptSellerModifyPrice(
        data,
        "acceptSellerModifyBasePriceByLine",
        customeracceptalterlineorderbaseprice
      );
    },
    // 买家拒绝修改线路订单基价
    rejectSellerModifyBasePriceByLine(data) {
      this.rejectSellerModifyPrice(
        data,
        "rejectSellerModifyBasePriceByLine",
        customerrejectalterlineorderbaseprice
      );
    },
    // 买家同意修改线路订单含税单价
    acceptSellerModifyFirmPriceByLine(data) {
      this.acceptSellerModifyPrice(
        data,
        "acceptSellerModifyFirmPriceByLine",
        customeracceptalterlineorderfirmprice
      );
    },
    // 买家拒绝修改线路订单含税单价
    rejectSellerModifyFirmPriceByLine(data) {
      this.rejectSellerModifyPrice(
        data,
        "rejectSellerModifyFirmPriceByLine",
        customerrejectalterlineorderfirmprice
      );
    }
  }
};
</script>
